package com.tenement.service.impl;

import com.github.pagehelper.PageHelper;
import com.google.gson.Gson;
import com.tenement.dao.TenementLogMapper;
import com.tenement.dao.TenementUserMapper;
import com.tenement.domain.TenementLog;
import com.tenement.domain.TenementUser;
import com.tenement.service.UserService;
import com.tenement.utils.Tools;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.*;

/**
 * @ClassName : UserServiceImpl  //类名
 * @Description : 用户服务接口实现类  //描述
 * @Author : 易嵘  //作者
 * @Date: 2020-06-06 13:42  //时间
 */
@Service
public class UserServiceImpl implements UserService {

    /**
     * 用户dao
     */
    @Resource
    TenementUserMapper userMapper;


    @Resource
    TenementLogMapper logMapper;
    /**
     * 登录判断
     * @param tenementUser 用户模型
     * @return 用户模型
     */
    @Override
    public TenementUser isLogin(TenementUser tenementUser) {
        return userMapper.isLogin(tenementUser);
    }

    /**
     * 查询所有（分页，条件）
     * @param user 用户模型
     * @param page 当前页数
     * @param limit 显示行数
     * @param startDate
     * @param endDate
     * @return
     */
    @Override
    public List<TenementUser> selecrAll(TenementUser user, Integer page, Integer limit, String startDate, String endDate) {
        PageHelper.startPage(page,limit);
        return userMapper.selectAll(user,startDate,endDate);
    }

    /**
     * 总行数
     * @param user 用户模型
     * @param startDate
     * @param endDate
     * @return
     */
    @Override
    public Long overCount(TenementUser user, String startDate, String endDate) {
        return userMapper.overCount(user,startDate,endDate);
    }

    /**
     * 删除
     * @param id
     * @param user
     * @param ip
     * @return
     */
    @Override
    public int deleteById(String id, TenementUser user, String ip) {
        TenementLog log=new TenementLog();
        log.setLogId(UUID.randomUUID().toString().replaceAll("-",""));
        log.setLogTitle("删除员工信息");
        StringBuffer logBody=new StringBuffer();
        Tools.logUserMod(userMapper.selectByPrimaryKey(id),logBody);
        log.setLogBody(logBody.toString());
        log.setLogType(4);
        log.setLogIp(ip);
        log.setCreatedBy(user.getUserName());
        logMapper.insertSelective(log);
        int row=userMapper.deleteById(id);
        if(row>0){
            userMapper.houseDeleteById(id);
        }
        return row;
    }

    /**
     * 批量删除
     * @param ids
     * @param user
     * @param ip
     * @return
     */
    @Override
    public int deleteAll(String[] ids, TenementUser user, String ip) {
        List<String> list=new ArrayList<>(Arrays.asList(ids));
        TenementLog log=new TenementLog();
        log.setLogId(UUID.randomUUID().toString().replaceAll("-",""));
        log.setLogTitle("批量删除员工信息");
        StringBuffer logBody=new StringBuffer();
        Tools.logUserMods(userMapper.findAllId(list),logBody);
        log.setLogBody(logBody.toString());
        log.setLogType(4);
        log.setLogIp(ip);
        log.setCreatedBy(user.getUserName());
        logMapper.insertSelective(log);
        int row=userMapper.updateAllPrimaryKey(list);
        row+=userMapper.houseDeleteAll(list);
        return row;
    }

    /**
     *
     * @param user 用户模型
     * @param user1 当前登录用户
     * @param ip
     * @return
     * @throws ParseException
     */
    @Override
    public int add(TenementUser user, TenementUser user1, String ip){
        user.setUserId(UUID.randomUUID().toString().replaceAll("-",""));
        user.setRevision(1);
        user.setCreatedBy(user1.getUserName());
        //user.setCreatedTime(new Date());
        //user.setUserType(1);
        TenementLog log=new TenementLog();
        log.setLogId(UUID.randomUUID().toString().replaceAll("-",""));
        log.setLogTitle("新增员工信息");
        StringBuffer logBody=new StringBuffer();
        Tools.logUserMod(user,logBody);
        log.setLogBody(logBody.toString());
        log.setLogType(1);
        log.setLogIp(ip);
        log.setCreatedBy(user1.getUserName());
        logMapper.insertSelective(log);
        return userMapper.insertSelective(user);
    }

    /**
     *
     * @param user 用户模型
     * @param user1 当前登录用户
     * @param ip
     * @return
     * @throws ParseException
     */
    @Override
    public int update(TenementUser user, TenementUser user1, String ip) {
        user.setUpdatedBy(user1.getUserName());
        user.setUpdatedTime(new Date());

        TenementLog log=new TenementLog();
        log.setLogId(UUID.randomUUID().toString().replaceAll("-",""));
        log.setLogTitle("修改员工信息");
        StringBuffer logBody=new StringBuffer();
        Tools.logUserMod(user,logBody);
        log.setLogBody(logBody.toString());
        log.setLogType(4);
        log.setLogIp(ip);
        log.setCreatedBy(user1.getUserName());
        logMapper.insertSelective(log);
        return userMapper.updateByPrimaryKeySelective(user);
    }

    /**
     * 查询所有
     * @return
     */
    @Override
    public List<TenementUser> findAll() {
        return userMapper.findAll();
    }

    /**
     * id查询
     * @param id
     * @return
     */
    @Override
    public TenementUser findById(String id) {
        return userMapper.selectByPrimaryKey(id);
    }


    @Override
    public int updatePwd(TenementUser user, String newUserPassword) {
        return 0;
    }

    @Override
    public TenementUser findWeiXin(TenementUser man) {
        return userMapper.findWeiXin(man);
    }

    @Override
    public List<TenementUser> findAlls() {
        return userMapper.findAlls();
    }
}
